//
//  IMMessageManager.m
//  TQY
//
//  Created by 甘世清 on 2024/10/28.
//

#import "IMMessageManager.h"
#import "LocationMessageTableViewCell.h"
#import "TextMessageTableViewCell.h"
#import "ImageMessageTableViewCell.h"
#import "IMContentManager.h"
@implementation IMMessageManager
+(CGFloat)SQ_getCellHeightMessageModel:(IMMessageModel *)messageModel
                             indexPath:(NSIndexPath*)indexPath
                             tableview:(UITableView *)tableview{
    Class cellClass = [IMMessageManager tableViewCellClassForMessegeModel:messageModel];
    
    return [cellClass FSCellHeightForTableView:tableview indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];;
}

+(BaseMessageTableViewCell*)SQ_createMessageCellForMessageModel:(IMMessageModel *)messageModel   array:(NSMutableArray*)array index:(NSInteger)index tableview:(UITableView *)tableview{
    NSString *cellId = [NSString stringWithFormat:@"BaseMessageCell_%@_%ld",messageModel.messType,(long)index];
    Class cellClass = [IMMessageManager tableViewCellClassForMessegeModel:messageModel];
    BaseMessageTableViewCell * _cell;
    //注册Cell
    _cell = [tableview dequeueReusableCellWithIdentifier:cellId];
    if (!_cell) {
        _cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    //判断是否是自己发的
     messageModel.isSend = [messageModel.fromID isEqualToString:[UserModel share].userid];//如果为否,则判断一下,
    if ([messageModel.messType isEqualToString:@"text"]) { //如果是文字才这样
        [IMContentManager SQ_settextLayout:messageModel];//设置contet转化后的textLayout
    }
    //判断是否显示时间,和时间格式
    [IMMessageManager SQ_newModel:messageModel array:array index:index];
    
    _cell.model = messageModel;
    return  _cell;
}
 

+ (Class)tableViewCellClassForMessegeModel:(IMMessageModel *)model {
    if ([model.messType isEqualToString:@"text"]) {
        return [TextMessageTableViewCell class];
    }
     else   if ([model.messType isEqualToString:@"img"]) {
        return [ImageMessageTableViewCell class];
    }
     else   if ([model.messType isEqualToString:@"location"]) {
           return [LocationMessageTableViewCell class];
    }
    return [BaseMessageTableViewCell class];
}

+(void)SQ_newModel:(IMMessageModel*)newModel array:(NSMutableArray*)array index:(NSInteger)index{
    newModel.showTime = [IMContentManager SQ_getTimeString:newModel.sendTime];
    if (index ==0) {//第一条显示时间
        newModel.isShowTime = YES;
    } else  {
        IMMessageModel * oldeModel = [array objectAtIndex:index-1];
        if ([newModel.sendTime longLongValue] - [oldeModel.sendTime longLongValue]  > 300) {
            newModel.isShowTime = YES;
        } else {
            newModel.isShowTime = NO;
        }
        //剩余时间
    }
}
 

/// 顶部显示聊天内容
/// - Parameter model: 聊天数据
+(void)SQ_showMessage:(IMMessageModel*)model{

    if ([model.sendTime isNotEmpty]) {
        int timeold = [model.sendTime intValue];
        int time = [[NSString SQ_currentTime] intValue];
        if (time - timeold > 60) {
            return; //超过1分钟的消息不提示
        }
    }
    UIViewController * vc = [Manager SQ_getCurrentViewController];
    if ([vc isKindOfClass:[IMMessageViewController class]]) {
        return;
    }
    
    MessageTopView * btn = [[MessageTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH-40, 64)];
    btn.model = model;
    LSTPopView * pop = [LSTPopView initWithCustomView:btn
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromTop dismissStyle:LSTDismissStyleSmoothToTop];
    pop.hemStyle = LSTHemStyleTop;
    pop.isStack = YES;
    pop.isHideBg = YES;
    pop.adjustY = TopHeight-40;
    pop.showTime = 2;
    [pop pop];
}
@end
